// let nodeData = {}
// nodeData.isViewNode = true;
// nodeData.name = "root";
// nodeData.childDic = {}



//----------创建S_genNode----------------------

function tree_genNode2(nodeData, parentDiv, onClick) {
    return tree_genNode(nodeData, parentDiv, null, onClick)
}

function tree_genNode(nodeData, parentDiv, parentNode = null, onClick = null) {
    //console.log("创建a......." + keyName)
    let wide = 200;
    let high = 15;
    let style_position = "absolute";//static,absolute,fixed,relative,inherit
    let o_nodeObj = {};
    o_nodeObj.keyName = nodeData.name;
    o_nodeObj.m_parentNode = parentNode;
    o_nodeObj.nodeData = nodeData;
    o_nodeObj.posX = 0;
    o_nodeObj.posY = 0;
    o_nodeObj.nodeCount = 0;
    o_nodeObj.isShow = true;
    o_nodeObj.onClick = onClick;


    let parentLine = document.createElement('a');
    o_nodeObj.m_parentLine = parentLine;
    parentLine.m_ode = o_nodeObj;
    parentLine.style.position = style_position;//绝对位置-非常重要的属性 
    parentLine.style.width = high + "px";
    parentLine.style.height = "1px";
    parentLine.style.zIndex = 0;
    parentLine.style.backgroundColor = "#776677";
    parentDiv.appendChild(parentLine);

    let subLine = document.createElement('a');
    o_nodeObj.m_subLine = subLine;
    subLine.o_nodeObj = o_nodeObj;
    subLine.style.position = style_position;//绝对位置-非常重要的属性 
    subLine.style.width = "1px";
    subLine.style.height = "1px";
    subLine.style.zIndex = 0;
    subLine.style.backgroundColor = "#776677";
    parentDiv.appendChild(subLine);


    let img = document.createElement('a');
    o_nodeObj.m_img = img;
    img.o_nodeObj = o_nodeObj;
    img.style.position = style_position;//绝对位置-非常重要的属性 
    img.style.width = high + "px";
    img.style.height = high + "px";
    img.style.zIndex = 0;
    img.style.backgroundColor = "#ffaa00";
    parentDiv.appendChild(img);

    let text = document.createElement('a');
    o_nodeObj.m_text = text;
    text.o_nodeObj = o_nodeObj;
    text.innerText = nodeData.name;
    text.style.position = style_position;//绝对位置-非常重要的属性 
    text.style.width = wide + "px";
    text.style.height = high + "px";
    text.style.zIndex = 0;
    text.style.fontSize = 0.01;
    text.style.backgroundColor = "#aaff99";
    parentDiv.appendChild(text);


    o_nodeObj.S_SetIsShow = function (isShow) {
        if (isShow) {
            this.m_parentLine.style.display = "block";
            this.m_subLine.style.display = "block";
            this.m_img.style.display = "block";
            this.m_text.style.display = "block";
        } else {
            this.m_parentLine.style.display = "none";
            this.m_subLine.style.display = "none";
            this.m_img.style.display = "none";
            this.m_text.style.display = "none";
        }
        if (this.m_childNodeArr != null && this.m_childNodeArr.length > 0) {
            for (let i3 = 0; i3 < this.m_childNodeArr.length; i3++) {
                this.m_childNodeArr[i3].S_SetIsShow(isShow);
            }
        }
    }

    o_nodeObj.S_Refresh = function (parentNodeObj, startPosX = 0, startPosY = 0, tagStr = "") {
        if (parentNodeObj == null) {
            parentNodeObj = {};
            parentNodeObj.posX = 0;
            parentNodeObj.posY = 0;
            parentNodeObj.nodeCount = 0;
        }
        parentNodeObj.nodeCount++;
        this.nodeCount = parentNodeObj.nodeCount;
        this.posX = parentNodeObj.posX + 20;
        this.posY = this.nodeCount * (high + 2);

        let offecttX = 0;
        this.m_parentLine.style.left = (startPosX + this.posX + offecttX) + "px";
        this.m_parentLine.style.top = (startPosY + this.posY + high * 0.5) + "px";
        offecttX += this.m_parentLine.offsetWidth;

        this.m_img.style.left = (startPosX + this.posX + offecttX) + "px";
        this.m_img.style.top = (startPosY + this.posY) + "px";
        offecttX += this.m_img.offsetWidth + 5;

        this.m_text.style.left = (startPosX + this.posX + offecttX) + "px";
        this.m_text.style.top = (startPosY + this.posY) + "px";
        tagStr += "___."
        //console.log(tagStr + this.keyName + "...." + this.posX + "...." + this.m_text.style.top + "....." + this.nodeCount);
        if (this.m_childNodeArr == null || this.m_childNodeArr.length == 0) {
            this.m_img.style.backgroundColor = "#cccccc";
        } else {
            if (this.isShow) {
                this.m_img.style.backgroundColor = "#ffaa00";
            } else {
                this.m_img.style.backgroundColor = "#ff33ff";
            }
        }

        let lastSubNpde = null;
        if (this.m_childNodeArr != null && this.m_childNodeArr.length > 0) {
            for (let i3 = 0; i3 < this.m_childNodeArr.length; i3++) {
                let childNode = this.m_childNodeArr[i3];
                childNode.S_SetIsShow(this.isShow);
                if (this.isShow) {
                    childNode.S_Refresh(this, startPosX, startPosY, tagStr);
                    parentNodeObj.nodeCount = childNode.nodeCount;
                    lastSubNpde = childNode;
                }
            }
        }
        if (lastSubNpde == null) {
            this.m_subLine.style.display = "none";
        } else {
            this.m_subLine.style.display = "block";
            this.m_subLine.style.width = "1px";
            this.m_subLine.style.height = (lastSubNpde.posY - this.posY) + "px";
            this.m_subLine.style.left = (startPosX + lastSubNpde.posX) + "px";
            this.m_subLine.style.top = (startPosY + this.posY + high * 0.5) + "px";
        }
        return this;
    }

    if (nodeData.isViewNode) {
        o_nodeObj.m_childNodeArr = [];
        let ii = 0;
        Object.keys(nodeData.childDic).forEach(function (key) {
            let node = tree_genNode(nodeData.childDic[key], parentDiv, o_nodeObj, onClick)
            o_nodeObj.m_childNodeArr[ii] = node;
            ii++;
        });
    }

    let S_onClick = function () {
        let nodeObj = this.o_nodeObj;
        if (nodeObj.m_childNodeArr == null || nodeObj.m_childNodeArr.length == 0) {
            return;
        }
        nodeObj.isShow = !nodeObj.isShow;
        //console.log("ddddd.............." + nodeObj.keyName + "......" + nodeObj.isShow)
        let parentNode = nodeObj;
        while (parentNode.m_parentNode != null) {
            parentNode = parentNode.m_parentNode;
            //console.log("ddddd..............." + parentNode.keyName)
        }
        if (parentNode.initOffectX == null) {
            parentNode.initOffectX = 0;
        }
        if (parentNode.initOffectY == null) {
            parentNode.initOffectY = 0;
        }
        parentNode.S_Refresh(null, parentNode.initOffectX, parentNode.initOffectY);
    }
    o_nodeObj.m_img.onclick = S_onClick;
    o_nodeObj.m_text.onclick = function () {
        if (o_nodeObj.onClick != null) {
            o_nodeObj.onClick(o_nodeObj.nodeData);
        }
    }

    return o_nodeObj;
}